package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.mapper.ClassChangeMapper;
import com.woniu.mapper.CoursesCardMapper;
import com.woniu.mapper.PrivateCoursesMapper;
import com.woniu.pojo.ClassChange;
import com.woniu.pojo.CoursesCard;
import com.woniu.pojo.PrivateTimetable;
import com.woniu.mapper.PrivateTimetableMapper;
import com.woniu.service.IPrivateTimetableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaohuang
 * @since 2022-10-12
 */
@Service
public class PrivateTimetableServiceImpl extends ServiceImpl<PrivateTimetableMapper, PrivateTimetable> implements IPrivateTimetableService {
    @Autowired
    private PrivateTimetableMapper privateTimetableMapper;
    @Autowired
    private ClassChangeMapper classChangeMapper;
    @Autowired
    private CoursesCardMapper coursesCardMapper;
    @Autowired
    private PrivateCoursesMapper privateCoursesMapper;
    @Override
    public PageResult search(Integer currentPage, Integer pageSize, PrivateTimetableQueryVo timetableQueryVo) {
        Page page = new Page(currentPage,pageSize);
        //关闭mybatis-plus对sql的自动优化（在count查询时舍弃掉join的内容）
        //解决方案一，把optimizeCountSql指定为false，
        //page.setOptimizeCountSql(false);
        //方案二，手写count sql
        page.setSearchCount(false);//关闭count sql
        IPage coursesIPage = privateTimetableMapper.findPage(page, timetableQueryVo);
        //调用手写count sql
        coursesIPage.setTotal(privateTimetableMapper.repListCount(timetableQueryVo));
        PageResult pageResult = new PageResult(coursesIPage.getTotal(), coursesIPage.getRecords());
        System.out.println("私课分页查询总数：" + coursesIPage.getTotal());
        System.out.println("总数：" + privateTimetableMapper.repListCount(timetableQueryVo));
        return pageResult;
    }

    @Override
    public Integer saveTimetable(PrivateTimetableAddVo privateTimetableAddVo) {
        PrivateTimetable privateTimetable = privateTimetableAddVo.getTimetable();
        CardChangeVo[] cardChangeVos = privateTimetableAddVo.getCardVos();
        //安排课程时进行教练和时间判断
        String coachId = privateTimetable.getCoachId();
        QueryWrapper<PrivateTimetable> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_id",coachId);
        wrapper.gt("end_date",privateTimetable.getStartDate());
        wrapper.gt("end_time",privateTimetable.getStartTime());
        wrapper.lt("start_date",privateTimetable.getEndDate());
        wrapper.lt("start_time",privateTimetable.getEndTime());
        Long count = privateTimetableMapper.selectCount(wrapper);
        System.out.println("时间冲突个数：" + count);
        if (count > 0){
            return 0;
        }
        Integer insert = privateTimetableMapper.insert(privateTimetable);
        String timetableId = privateTimetable.getId();

        if (cardChangeVos.length > 0){
            for (CardChangeVo cardChangeVo : cardChangeVos) {
                ClassChange classChange = new ClassChange();
                CoursesCard coursesCard = new CoursesCard();
                String cardId = cardChangeVo.getId();
                classChange.setOneClassHour(cardChangeVo.getOneClassHour());
                classChange.setSixClassHour(cardChangeVo.getSixClassHour());
                classChange.setTenClassHour(cardChangeVo.getTenClassHour());
                int insert1 = classChangeMapper.insert(classChange);

                String classChangeId = classChange.getId();
                coursesCard.setCardId(cardId);
                coursesCard.setClassChangeId(classChangeId);
                coursesCard.setTimetableId(timetableId);
                int insert2 = coursesCardMapper.insert(coursesCard);
            }
        }
        return insert;
    }

    @Override
    public Integer deleteById(String id) {
        //查询课程课时表
        QueryWrapper<CoursesCard> wrapper = new QueryWrapper<>();
        wrapper.eq("timetable_id",id);
        List<CoursesCard> coursesCards = coursesCardMapper.selectList(wrapper);
        if (coursesCards != null){
            coursesCards.forEach(coursesCard -> {
                System.out.println("删除的课时费id：" + coursesCard.getClassChangeId());
                int i = classChangeMapper.deleteById(coursesCard.getClassChangeId());
                System.out.println("是否删除课时费：" + i);
            });
            int delete = coursesCardMapper.delete(wrapper);

        }
        int i = privateTimetableMapper.deleteById(id);

        return i;
    }

    @Override
    public PageResult findByGymId(String gymId) {
        Page<AppCoursesQueryVo> page = new Page<>(1, 6);
        page.setOptimizeCountSql(false);
        IPage coursesPage = privateTimetableMapper.findByGymId(page,gymId);
        PageResult pageResult = new PageResult(coursesPage.getTotal(), coursesPage.getRecords());
        return pageResult;
    }

    @Override
    public AppChangeQueryVo findByTimetableId(String id) {
        AppChangeQueryVo appChangeQueryVo =privateTimetableMapper.findByTimetableId(id);
        return appChangeQueryVo;
    }

}
